package com.tuoyu.peopletest.utils;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import java.io.ByteArrayOutputStream;

/**
 * 图片价值类
 */
public class SCImageUtils {

    public static void load(Context context, Object res, ImageView iv) {
        Glide.with(context).load(res).into(iv);
    }




//    public static void load(Context context, Object o, final OnDataListener listener) {
//
//        Glide.with(context).load(o).into(new SimpleTarget<Drawable>() {
//            @Override
//            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
//                listener.onData(resource);
//            }
//        });
//    }



    public static void loadCenterCrop(Context context, Object o, ImageView iv) {
        RequestOptions options = new RequestOptions();
        options.centerCrop();
        Glide.with(context).load(o).apply(options).into(iv);
    }

    /**
     * 有占位图
     *
     * @param context
     * @param o
     * @param iv
     * @param nomalRes
     */
    public static void loadCenterCrop(Context context, Object o, ImageView iv, int nomalRes) {
        RequestOptions options = new RequestOptions()
                .placeholder(nomalRes);
        options.centerCrop();
        Glide.with(context)
                .load(o)
                .apply(options)
                .into(iv);

    }


    /**
     * 普通加载图片
     * 拉伸
     * @param context
     * @param o
     */
    public static void loadfitCenter(Context context, Object o, ImageView iv) {
        RequestOptions options = new RequestOptions();
        options.fitCenter();
        Glide.with(context).load(o).apply(options).into(iv);
    }



    /**
     * 加载清晰的图
     *
     * @param context
     * @param o
     * @param iv
     */
    public static void loadHigh(Context context, Object o, ImageView iv) {
        RequestOptions options = new RequestOptions();

        options.format(DecodeFormat.PREFER_ARGB_8888);
        Glide.with(context).load(o).apply(options).into(iv);
    }

    /**
     * 加载清晰的图
     *
     * @param context
     * @param o
     * @param iv
     */
    public static void loadHigh(Context context, Object o, ImageView iv,int nomalRes) {
        RequestOptions options = new RequestOptions();
        options.format(DecodeFormat.PREFER_ARGB_8888);
        options.placeholder(nomalRes);
        Glide.with(context).load(o).apply(options).into(iv);
    }

    public static void loadHigh(Context context, Object o, final OnDataListener listener) {
        RequestOptions options = new RequestOptions();
        options.format(DecodeFormat.PREFER_ARGB_8888);
        Glide.with(context).load(o).apply(options).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                listener.onData(resource);
            }
        });
    }

    public static void loadHigh02(Context context, Object o,  final OnDataBtListener listener) {
        RequestOptions options = new RequestOptions();
        options.format(DecodeFormat.PREFER_ARGB_8888);
        Glide.with(context)
                .asBitmap()
                .load(o)
                .apply(options)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady( Bitmap resource, Transition<? super Bitmap> transition) {
                        listener.onBitmapData(resource);
                    }
                });
    }

    public static void loadCircle(Context context, Object o, ImageView iv) {
        RequestOptions options = new RequestOptions();
        options.centerCrop().transform(new CircleCrop());
        Glide.with(context).load(o).apply(options).into(iv);
    }

    public static void loadHeightCircle(Context context, Object o, ImageView iv) {
        RequestOptions options = new RequestOptions();
        options.circleCrop();
        options.format(DecodeFormat.PREFER_ARGB_8888);
        Glide.with(context).load(o).apply(options).into(iv);
    }





    public static void loadHighBitmap(Context context, Object o, ImageView iv, int nomalRes) {
        RequestOptions options = new RequestOptions();
        options.format(DecodeFormat.PREFER_ARGB_8888);
        options.placeholder(nomalRes);
        Glide.with(context)
                .asBitmap()
                .load(o)
                .apply(options)
                .into(iv);
    }

    public static void loadBitmap(Context context, Object o, ImageView iv, int nomalRes) {
        RequestOptions options = new RequestOptions()
                .placeholder(nomalRes);
        Glide.with(context)
                .asBitmap()
                .load(o)
                .apply(options)
                .into(iv);
    }

    /**
     * 加载清晰的图
     *
     * @param context
     * @param o
     * @param iv
     */
    public static void loadHighCenterCrop(Context context, Object o, ImageView iv) {
        RequestOptions options = new RequestOptions();
        options.format(DecodeFormat.PREFER_ARGB_8888);
        options.centerCrop();
        Glide.with(context).load(o).apply(options).into(iv);
    }

    public static void loadHighCenterCrop(Context context, Object o, ImageView iv, int nomalRes) {
        RequestOptions options = new RequestOptions()
                .placeholder(nomalRes);
        options.format(DecodeFormat.PREFER_ARGB_8888);
        options.centerCrop();

        Glide.with(context)
                .load(o)
                .apply(options)
                .into(iv);

    }

    /**
     * 加载清晰的图
     *
     * @param context
     * @param o
     * @param iv
     */
    public static void loadHighFitCenter(Context context, Object o, ImageView iv) {
        RequestOptions options = new RequestOptions();
        options.format(DecodeFormat.PREFER_ARGB_8888);
        options.fitCenter();
        Glide.with(context).load(o).apply(options).into(iv);
    }

    /**
     * 加载清晰的图
     *
     * @param context
     * @param o
     * @param iv
     */
    public static void loadHighCenterInside(Context context, Object o, ImageView iv) {
        RequestOptions options = new RequestOptions();
        options.format(DecodeFormat.PREFER_ARGB_8888);
        options.centerInside();
        Glide.with(context).load(o).apply(options).into(iv);
    }


    public interface OnDataListener {
        void onData(Drawable resource);
    }

    public interface OnDataBtListener {
        void onBitmapData(Bitmap resource);
    }

    public interface OnDataPathListener {
        void onData(String resource);
    }

    /**
     * 预加载
     *
     * @param context
     * @param o
     */
    public static void loadPreload(Context context, Object o) {
        RequestOptions options = new RequestOptions();
        options.format(DecodeFormat.PREFER_ARGB_8888);
        Glide.with(context)
                .load(o)
                .apply(options)
                .preload();
    }

//    public static void loadSubmit(Context context, Object o, OnDataPathListener listener) {
//        RequestOptions options = new RequestOptions();
//        options.format(DecodeFormat.PREFER_ARGB_8888);
//        MyThreadPoolManager.getInstance().execute(new Runnable() {
//            @Override
//            public void run() {
//                try {
//
//                    FutureTarget<File> target = Glide.with(context)
//                            .asFile()
//                            .load(o)
//                            .apply(options)
//                            .submit();
//                    final File imageFile = target.get();
//                    UiUtils.runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            listener.onData(imageFile.getPath());
//                        }
//                    });
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }


    /**
     * 大小压缩
     * maxByteSize  1000kb*1024b
     */
    public static byte[] getCompressByQualityByte(final Bitmap src,
                                                  final long maxByteSize) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        src.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] bytes;
        //它是该图片实际的大小（就是占手机电脑上的存储空间）
        //baos.toByteArray().length
        //bitmap.getByteCount(),Bitmap在内存中的大小（他比baos的字节数大了好几个数量级，OOM内存溢出说的就是这货占地太大了！）
        if (baos.size() <= maxByteSize) {

            bytes = baos.toByteArray();
        } else {
            baos.reset();
            src.compress(Bitmap.CompressFormat.JPEG, 0, baos);
            if (baos.size() >= maxByteSize) {
                bytes = baos.toByteArray();
            } else {
                // find the best quality using binary search
                int st = 0;
                int end = 100;
                int mid = 0;
                while (st < end) {
                    mid = (st + end) / 2;
                    baos.reset();
                    src.compress(Bitmap.CompressFormat.JPEG, mid, baos);
                    int len = baos.size();
                    if (len == maxByteSize) {
                        break;
                    } else if (len > maxByteSize) {
                        end = mid - 1;
                    } else {
                        st = mid + 1;
                    }
                }
                if (end == mid - 1) {
                    baos.reset();
                    src.compress(Bitmap.CompressFormat.JPEG, st, baos);
                }
                bytes = baos.toByteArray();
            }
        }

        return bytes;
    }

}
